package com.jkd.taos.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.jkd.taos.domain.SourceData;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
@DS("tdengine")
@InterceptorIgnore(tenantLine = "true")
public interface SourceDatamapper extends BaseMapper<SourceData> {

    String DB_NAME = "jkiot";

//    @Update("CREATE TABLE if not exists temperature(ts timestamp, temperature float) tags(location nchar(64), tbIndex int)")
//    int createSuperTable();
//
//    @Update("create table #{tbName} using temperature tags( #{location}, #{tbindex})")
//    int createTable(@Param("tbName") String tbName, @Param("location") String location, @Param("tbindex") int tbindex);
//
//    @Update("drop table if exists temperature")
//    void dropSuperTable();

    //    @Insert("insert into t${tbIndex}(ts, temperature) values(#{ts}, #{temperature})")
    @Insert("INSERT INTO " + DB_NAME + ".t_#{sensorNum} USING " + DB_NAME + ".source_data TAGS (#{sensorNum}) VALUES (#{addTime}, #{dataOne,jdbcType=DOUBLE}, #{unitOne,jdbcType=VARCHAR}, #{dataTwo,jdbcType=DECIMAL},#{unitTwo,jdbcType=VARCHAR},#{dataThree,jdbcType=DECIMAL},#{unitThree,jdbcType=VARCHAR},#{voltage,jdbcType=DECIMAL},#{temperature,jdbcType=DECIMAL});")
    int insertOne(SourceData one);

    @Select("select count(1) from source_data")
    int sourceDataCount();

    @Select("select count(1) from sys_tenant")
    int usercount();

    @Select("select * from " + DB_NAME + ".source_data ${ew.customSqlSegment}")
    List<SourceData> selectHistoryList(@Param(Constants.WRAPPER) LambdaQueryWrapper wrapper);

    @Select("select last_row(add_time) as add_time,last_row(data_one) as data_one,last_row(unit_one) as unit_one,last_row(data_two) as data_two,last_row(unit_two) as unit_two,last_row(data_three) as data_three,last_row(unit_three),last_row(voltage) as voltage,last_row(temperature) as temperature from " + DB_NAME + ".source_data where sensor_num=#{sensorNum}")
    SourceData selectLatestOne(String sensorNum);

    @Select("select count(1) from " + DB_NAME + ".source_data ${ew.customSqlSegment}")
    Long sensorTotal(@Param(Constants.WRAPPER) LambdaQueryWrapper wrapper);

    @Select("select * from " + DB_NAME + ".source_data ${ew.customSqlSegment}")
    List<SourceData> sensorDataList(@Param(Constants.WRAPPER) LambdaQueryWrapper<SourceData> queryWrapper);
}
